<?php
require_once("../DAO/UserDAO.php");
include_once("../fckeditor/fckeditor.php") ;
include_once("../DAO/ParagraphDAO.php");
include_once("../Utility/WebUtilities.php");
include_once("../Utility/WebSession.php");
include_once("../Utility/FunctionUtilities.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

if(isset($_POST["FCKeditor1"]) && $_POST["FCKeditor1"] != "")
{
	$paragraphDAO = new ParagraphDAO();
	if(!isset($_GET["id"]))
	{
		$item = new Paragraph();
		$item->Content = $_POST["FCKeditor1"];
		$item->Sound = FunctionUtilities::SaveUploadSound($_FILES['uploadfile'], false);
		$paragraphDAO->CreateParagraph($item);
	}
	else
	{
		$item = $paragraphDAO->GetParagraphById($_GET["id"]);
		if($item != null)
		{
			$item->Content = $_POST["FCKeditor1"];
			$oldPath = $item->Sound;
			$item->Sound = FunctionUtilities::SaveUploadSound($_FILES['uploadfile'], false);
			if($item->Sound != "")
				FunctionUtilities::DeleteFile($oldPath);
			else
				$item->Sound = $oldPath;
			$paragraphDAO->UpdateParagraph($item);
		}
	}
	WebUtilities::Redirect("ParagraphManagement.php");
}

$oFCKeditor = new FCKeditor('FCKeditor1') ;
$oFCKeditor->BasePath = '../fckeditor/' ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = '78%';
$oFCKeditor->Height = '400';

$submitUrl = "EditParagraph.php";
$questionStr = "";
if(isset($_GET["id"]))
{
	$paragraphDAO = new ParagraphDAO();
	$item = $paragraphDAO->GetParagraphById($_GET["id"]);
	if($item != null)
	{
		$oFCKeditor->Value = $item->Content;
		$submitUrl = "EditParagraph.php?id=" . $item->Id;
		$questionStr = "<a class='right' href='#' onclick=\"window.open('QuestionManagement.php?paragraphId=" . $item->Id . "')\">Questions of this paragraph</a>";
	}
	else
	{
		$questionStr = "<a class='right' href='#'>After you create paragraph , you can add questions for this paragraph</a>";
	}
}
else
{
	$questionStr = "<a class='right' href='#'>After you create paragraph , you can add questions for this paragraph</a>";
}

$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
				<div class='oneLine'>
					<div class='left'>Sound</div>
					<input class='right' type='file' name='uploadfile'/>
				</div>
				<div class='oneLine'>
					<div class='left'>Questions</div>" . $questionStr .
				"</div>
				<div class='oneLine'>
					<div class='left'>Content</div>";

$mainContent = $mainContent . $oFCKeditor->CreateHtml() . "</div>
				<div class='oneLine'>
					<div class='left'>&nbsp;</div>
					<div>
						<input type='submit' value='Save' name='btSave'/>
						<input type='button' value='Cancel' onclick=\"window.location='ParagraphManagement.php'\" />
					</div>
				</div></form>";
$pageIndex = 4;
include("MasterPage.php");

?>